﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BulldogCMS.Core;
using BulldogCMS.Core.Storage.DataTypes;

namespace BulldogCMS.Manager.Models
{
    public class UsersDAO : IUsersDAO
    {
        public User GetUser(string email)
        {
            var userNode = Context.Application.Tree.GetNode("/System/Users/" + email);
            if (userNode != null)
            {
                User user = new User()
                {
                    PasswordHash = userNode.GetValue<TextField>("PasswordHash").Value,
                    Email = userNode.GetValue<TextField>("Email").Value
                };
                return user;
            }
            return null;
        }

        public IEnumerable<User> GetUsers()
        {
            var usersNode = Context.Application.Tree.GetNode("/System/Users");
            if (usersNode != null)
            {
                foreach (var userNode in usersNode.Nodes)
                {
                    yield return userNode.As<User>();
                }
            }
        }

    }

    public interface IUsersDAO
    {
        User GetUser(string email);
        IEnumerable<User> GetUsers();
    }
}